package br.com.gsh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.gsh.conexao.ConexaoFactory;
import br.com.gsh.pojo.Twitter;

public class TwitterDao {
	
	private Connection connection;

	public TwitterDao() {
		this.connection = new ConexaoFactory().getConexao();
	}
	
	public void adiciona(Twitter twitter) throws SQLException {

		String sql = "insert into twitters " +
					"(mensagem, id_usuario, pergunta, data, hora, data_hora, status)" +
					" values (?, ?, ?, now(), now(), now(), 0)";
		PreparedStatement ps = connection.prepareStatement(sql);

		ps.setString(1, twitter.getMensagem());
		ps.setString(2, twitter.getIdUsuario());
		ps.setString(3, twitter.getPergunta());

		ps.execute();
		ps.close();

	}
	
	public void adicionaReTuit(Twitter twitter) throws SQLException {

		String sql = "insert into twitters " +
					"(mensagem, id_usuario, reTuitou, pergunta, data, hora, data_hora, status)" +
					" values (?, ?, ?, 0, now(), now(), now(), 0)";
		PreparedStatement ps = connection.prepareStatement(sql);

		ps.setString(1, twitter.getMensagem());
		ps.setString(2, twitter.getIdUsuario());
		ps.setString(3, twitter.getReTuitou());

		ps.execute();
		ps.close();

	}
	
	public List<Twitter> listar() throws SQLException {

		String sql = "select * from twitters order by data_hora desc";
		PreparedStatement ps = connection.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();

		List<Twitter> twiter = new ArrayList<Twitter>();
		Twitter twitter = null;

		while (rs.next()) {
			
			twitter = new Twitter();
			
			twitter.setId(rs.getInt("id"));
			twitter.setMensagem(rs.getString("mensagem"));
			twitter.setIdUsuario(rs.getString("id_usuario"));
			twitter.setData(rs.getDate("data"));
			twitter.setHora(rs.getString("hora"));
			twitter.setDataHora(rs.getDate("data_hora"));
			twitter.setResposta(rs.getString("resposta"));
			twitter.setPergunta(rs.getString("pergunta"));
			twitter.setStatus(rs.getInt("status"));

			twiter.add(twitter);

			System.out.println("tuiter: " + twitter.getMensagem());
		}

		ps.execute();
		ps.close();

		return twiter;

	}
	
	public List<Twitter> listaTuitProprio(String nick) throws SQLException {

		
		//testar depois -- só add or retuitou = ?
		String sql = "select * from twitters where id_usuario = ? or reTuitou = ? order by data_hora desc";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, nick);
		
		ResultSet rs = ps.executeQuery();

		List<Twitter> twiter = new ArrayList<Twitter>();
		Twitter twitter = null;

		while (rs.next()) {
			
			twitter = new Twitter();
			
			twitter.setId(rs.getInt("id"));
			twitter.setMensagem(rs.getString("mensagem"));
			twitter.setIdUsuario(rs.getString("id_usuario"));
			twitter.setData(rs.getDate("data"));
			twitter.setHora(rs.getString("hora"));
			twitter.setDataHora(rs.getDate("data_hora"));
			twitter.setResposta(rs.getString("resposta"));
			twitter.setPergunta(rs.getString("pergunta"));
			twitter.setStatus(rs.getInt("status"));

			twiter.add(twitter);

			System.out.println("tuiter: " + twitter.getMensagem());
		}

		ps.execute();
		ps.close();

		return twiter;

	}
	
	 public void alterar(Twitter twitter) throws SQLException {
         
         String sql = "update twitters set mensagem = ?, data = now(), hora = now(), data_hora = now() where id = ?";
         PreparedStatement ps = connection.prepareStatement(sql);

         ps.setString(1, twitter.getMensagem());
         ps.setInt(5, twitter.getId());

         ps.execute();
         ps.close();
         
	 }
	 
	 public void atualizaResp(Twitter twitter) throws SQLException {
         
         String sql = "update twitters set resposta = ? where id = ?";
         PreparedStatement ps = connection.prepareStatement(sql);

         ps.setString(1, twitter.getResposta());
         ps.setInt(2, twitter.getId());

         ps.execute();
         ps.close();
         
	 }
	

}
